﻿using rbac.Contracts.DTO.Input;
using rbac.Contracts.DTO.Output;
using rbac.Contracts.Interface;
using rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Services
{
    public class RolePerMissionService:ServiceBase,IRolePerMissionService
    {
        private readonly IRolePermissionRepository _rolePermissionRepository;

        public RolePerMissionService(IRolePermissionRepository rolePermissionRepository)
        {
            _rolePermissionRepository = rolePermissionRepository;
        }
        public int DistributeRolePermission(DistributeRolePermissionsInput input)
        {
            return _rolePermissionRepository.DistributeRolePermission(input.RoleId,input.PermissionList);
        }
        /// <summary>
        /// 获取角色获取权限列表
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<PermissionDto> GetPermissionByRoleId(int roleId)
        {
            //获取权限列表
            var permissions=_rolePermissionRepository.GetPermissionByRoleId(roleId);
            List<PermissionDto> result = new();
            foreach (var permission in permissions.Where(c=>c.ParentId==0))
            {
                var dto =_mapper.Map<PermissionDto>(permission);
                //获取权限子菜单
                var child = permissions.Where(p => p.ParentId == permission.Id).ToList();
                if(child.Any())
                {
                    dto.Child=_mapper.Map<List<PermissionDto>>(child);
                }
                result.Add(dto);

            }
            return result;
        }
    }
}
